package com.demo.chat.listen;
/*
 *ClassName:STOMPConnectEventListener
 *Description:
 *@author:可爱的大鸭子
 */

import com.demo.chat.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectEvent;

@Component
public class STOMPConnectEventListener implements ApplicationListener<SessionConnectEvent> {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Override
    public void onApplicationEvent(SessionConnectEvent event) {
        StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
        System.out.println("login:"+sha.getNativeHeader("login"));
        String userId = sha.getNativeHeader("login").get(0);
        String sessionId = sha.getSessionId();
        System.out.println("STOMPConnectEventListener........"+userId+"-"+sessionId);
        Application.sessionMap.put(userId,sessionId);
    }
}